﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
 namespace hwms {
    public  class TaskNoUtil  {
        public static  int getTaskNo(IDbConnection conn ,IDbTransaction trans) {
            int value = 1;
            DateTime dt = DateTime.Now;
            String dateline = dt.ToString("yyyyMMdd");
            int now = int.Parse(dateline);
            string sql1 = "SELECT v FROM b_Task_No WHERE k= @now";
              
            List<int> tc = conn.Query<int>(sql1 ,new { now  } ,trans).AsList();
            if( tc.Count == 0 ) {
                conn.Execute("insert into b_Task_No (k,v) values (@now,@value)" ,new { now ,value } ,trans);
            } else {
                value = tc[0] + 1;
                string sql2 = "UPDATE b_Task_No SET v=@v WHERE k=@now";
                conn.Execute(sql2 ,new { v = value ,now } ,trans);
            }
            string begin = now%10000+"";
            if( value < 10 ) {
                begin += "00" + value;
            } else if( value < 100 ) {
                begin += "0" + value;
            } else {
                begin += value;
            }
            return int.Parse(begin);
        }
    }
}
